package com.goldplusgold.live.channel;

import android.text.TextUtils;

import com.goldplusgold.live.bean.LMessageBean;
import com.goldplusgold.live.protobuf.ChatProto;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * ResponseHandler
 * Created by Administrator on 2017/7/13.
 */
public class ResponseHandler extends SimpleChannelInboundHandler<ChatProto.ChatResponse> {

    public ReceiverMessageInterface receiverMessageInterface;

    public ResponseHandler(ReceiverMessageInterface receiverMessageInterface){
        this.receiverMessageInterface = receiverMessageInterface;

    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatProto.ChatResponse response) throws Exception {
        handleBroadcast(ctx, response);
    }

    private void handleResponse(ChannelHandlerContext ctx, ChatProto.ChatResponse response) {
        switch (response.getCode()){
            case RESPONSE_SUCCESS:
                break;
        }
    }

    private void handleBroadcast(ChannelHandlerContext ctx, ChatProto.ChatResponse broadcast) {
        ChatProto.ChatResponse response = (ChatProto.ChatResponse)broadcast;
        switch (response.getCode()){
            case RESPONSE_SUCCESS:
                break;
            case BROADCAST_SEND:
                LMessageBean bean = new LMessageBean();
                bean.setContent(response.getSendBroadcastData().getContent());
                bean.setTime(response.getSendBroadcastData().getTime());
                bean.setId(response.getSendBroadcastData().getId());
                bean.setUserId(response.getSendBroadcastData().getUserId());
                bean.setUserName(response.getSendBroadcastData().getUserName());
                bean.setMediaType(response.getSendBroadcastData().getMediaType().getNumber());
                bean.setMediaUri(response.getSendBroadcastData().getMediaUri());
                if(receiverMessageInterface != null){
                    receiverMessageInterface.onReceiver(bean);
                }
                break;
            case BROADCAST_STEP_IN:
                break;
            case BROADCAST_STEP_OUT:
                break;
            case BROADCAST_TAKE_BACK:
                if(receiverMessageInterface != null){
                    receiverMessageInterface.onTakeBack(response.getTakeBackBroadcastData().getId());
                }
                break;
        }
    }


    public interface ReceiverMessageInterface{
        void onReceiver(LMessageBean bean);
        void onTakeBack(String id);
    }
}
